package lac.serviceuint.test;

import java.io.FileNotFoundException;

import org.junit.Test;

import com.thoughtworks.xstream.XStream;

import lac.serviceuint.cfg.Method;
import lac.serviceuint.cfg.ServiceUnit;
import lac.serviceuint.job.cfg.ServiceUnitJob;
import lac.serviceuint.job.cfg.ServiceUnitJobParam;

public class TextXML {

	
	protected static XStream stream = new XStream();
	static{
		stream.processAnnotations(ServiceUnitJob.class);
		stream.processAnnotations(ServiceUnit.class);
		
		//xstream.setMode(XStream.NO_REFERENCES);
	}
	
	@Test
	public void testToXml() {
		ServiceUnit serviceUnit = new ServiceUnit();
		serviceUnit.setName("UserService");
		Method method = new Method("save");
		serviceUnit.getMethods().add(method);

		stream.toXML(serviceUnit, System.out);
	}
	
	@Test
	public void testTaskXml(){
		ServiceUnitJob serviceUnitTask = new ServiceUnitJob();
		serviceUnitTask.setName("updateSettleStatusJob");
		serviceUnitTask.setServiceUnitName("SettlementOrderService");
		serviceUnitTask.setMethodName("updateSettleStatusJob");
		serviceUnitTask.setCron("* 0/5 * * * ?");
		serviceUnitTask.getParams().add(new ServiceUnitJobParam("d", "String", 4, "d"));
		serviceUnitTask.getParams().add(new ServiceUnitJobParam("d", "String", 4, "d"));
		
		stream.toXML(serviceUnitTask, System.out);
	}

	@Test
	public void testServiceUnitFromXml() throws FileNotFoundException {
		//ServiceUnit serviceUnit= (ServiceUnit) stream.fromXML(getClass().getResourceAsStream("/xml/service.xml"));
		ServiceUnit serviceUnit= (ServiceUnit) stream.fromXML(getClass().getResourceAsStream("/xml/FeedbackService.xml"));
		
		stream.toXML(serviceUnit, System.out);
	}
	
	
	@Test
	public void testTaskFromXml() throws FileNotFoundException {
		
		ServiceUnitJob task= (ServiceUnitJob) stream.fromXML(getClass().getResourceAsStream("/xml/task.xml"));
		stream.toXML(task, System.out);
	}
	
	
}
